/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.program;

import java.util.List;

import net.bioteam.appweb.gwt.apps.client.Util.ApplicationCallBack;
import net.bioteam.appweb.gwt.apps.client.Util.GwtRpcDataSource;
import net.bioteam.appweb.gwt.apps.clientandserver.program.AppNode;
import net.bioteam.appweb.gwt.apps.clientandserver.program.AppTreeProxy;
import net.bioteam.appweb.gwt.apps.clientandserver.program.AppTreeProxyAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeNode;

public class AppTreeDataSource extends GwtRpcDataSource
{

	private int myid=20;
	private int getMyId()
	{
		return myid++;
	}
	public AppTreeDataSource()
	{

		super();

		DataSourceIntegerField idField = new DataSourceIntegerField("id");
		idField.setPrimaryKey(true);

		DataSourceTextField textField = new DataSourceTextField("text");
		DataSourceTextField nameField = new DataSourceTextField("name");
		DataSourceTextField tipField = new DataSourceTextField("tip");
		DataSourceTextField folderField = new DataSourceTextField("folder");

		DataSourceTextField ptype = new DataSourceTextField("ptype");
		DataSourceTextField pvalue = new DataSourceTextField("pvalue");
		setFields(idField, textField, nameField, folderField, ptype, pvalue, tipField);

	}
	
	Tree tree;
	public void setTree(Tree t)
	{
		tree=t;
	}

	@Override
	protected void executeAdd(String requestId, DSRequest request, DSResponse response)
	{
		// TODO Auto-generated method stub

	}

	private void copyValues(AppNode node, TreeNode record)
	{

		record.setAttribute("text", node.text);
		record.setAttribute("name", node.text);
		record.setAttribute("ptype", node.ptype);
		record.setAttribute("pvalue", node.pvalue);
		record.setAttribute("tip", node.tip);
		record.setAttribute("folder", !node.leaf);
		// need to set id based on request
		record.setAttribute("id", getMyId());

	}
	@Override
	protected void executeFetch(final String requestId, final DSRequest request, final DSResponse response)
	{
		JavaScriptObject obj = request.getAttributeAsJavaScriptObject("parentNode");
		final TreeNode parentNode = new TreeNode(obj);
		//TreeGrid grid = (TreeGrid) Canvas.getById(request.getComponentId());
		final String parentPath=tree.getPath(parentNode);
		
		final String parentId = parentNode.getAttribute("id");

		ApplicationCallBack<List<AppNode>> callback = new ApplicationCallBack<List<AppNode>>()
		{

			public void onFailureAfter(Throwable caught)
			{
				response.setStatus(RPCResponse.STATUS_FAILURE);
				processResponse(requestId, response);

			}

			public void onSuccessAfter(List<AppNode> result)
			{
				ListGridRecord[] list = new ListGridRecord[result.size()];
				for (int i = 0; i < list.length; i++)
				{
					TreeNode record = new TreeNode();
					AppNode node = result.get(i);
					copyValues(node, record);
					record.setParentID(parentId);
					list[i] = record;

				}

				response.setData(list);
				processResponse(requestId, response);

			}

			public void execute()
			{
				AppTreeProxyAsync appTreeProxy = GWT.create(AppTreeProxy.class);
				appTreeProxy.getNodes(parentPath, this);

			}

		};
		callback.execute();

	}

	@Override
	protected void executeRemove(String requestId, DSRequest request, DSResponse response)
	{
		// TODO Auto-generated method stub

	}

	@Override
	protected void executeUpdate(String requestId, DSRequest request, DSResponse response)
	{
		// TODO Auto-generated method stub

	}
}
